package com.demo.autotest.service;

import com.demo.autotest.database.domain.Role;
import com.demo.autotest.database.domain.User;
import com.demo.autotest.database.domain.UserProject;
import com.demo.autotest.database.domain.UserRole;
import com.demo.autotest.database.mapper.ProjectMapper;
import com.demo.autotest.database.mapper.RoleMapper;
import com.demo.autotest.dto.RoleDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.request.RoleUserRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service    // 定义service层
@Transactional(rollbackFor = Exception.class)
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private ProjectMapper projectMapper;


//    public void saveRole(Role role){
//        // 验证account、mobile是否已经存在数据库中 todo
//
//        if(role.getId() ==null || role.getId().equals("")){
//            role.setId(UUID.randomUUID().toString());
//            role.setCreateTime(System.currentTimeMillis());
//            role.setUpdateTime(System.currentTimeMillis());
//            roleMapper.insertRole(role);
//        }else {
//            role.setUpdateTime(System.currentTimeMillis());
//            roleMapper.updateRole(role);
//        }
//    }

    public void deleteRoleUser(UserRole userRole){
        Role role = roleMapper.getRoleById(userRole.getRoleId());
        roleMapper.deleteRoleUser(userRole);
        Integer count = roleMapper.countRoleUserByProject(role.getProjectId(), userRole.getUserId());

        if (count == 0){
            projectMapper.deleteProjectUser(role.getProjectId(), userRole.getUserId());
        }
    }

    public void addRoleUser(RoleUserRequest request){
        List<UserRole> userRoles = new ArrayList<>();
        Role role = roleMapper.getRoleById(request.getRoleId());
        for (String userId: request.getUserIds()){
            Integer count = roleMapper.countRoleUser(request.getRoleId(), userId);
            if (count>0){
                continue;
            }
            UserRole userRole = new UserRole();
            userRole.setId(UUID.randomUUID().toString());
            userRole.setRoleId(request.getRoleId());
            userRole.setCreateTime(System.currentTimeMillis());
            userRole.setUpdateTime(System.currentTimeMillis());
            userRoles.add(userRole);
            Integer count1 = projectMapper.countProjectUser(role.getProjectId(), userId);
            if (count1 == 0){
                UserProject userProject = new UserProject();
                userProject.setId(UUID.randomUUID().toString());
                userProject.setUserId(userId);
                userProject.setProjectId(role.getProjectId());
                userProject.setCreateTime(System.currentTimeMillis());
                userProject.setUpdateTime(System.currentTimeMillis());
                projectMapper.insertProjectUser(userProject);
            }
        }
        if (userRoles.size()>0){
            roleMapper.insertRoleUser(userRoles);
        }
    }

    public List<User> getRoleUser(String roleId){
        return roleMapper.getRoleUser(roleId);
    }

    public List<RoleDTO> getRoleList(QueryRequest request){
        if (request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition("%"+request.getCondition()+"%");
        }
        return roleMapper.getRoleList(request);
    }

}
